<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>综合案例-成绩案例</title>
    <style>
        .red {
            color: red;
        }
    </style>
</head>

<body>
    <!-- 
        需求：
        1.渲染功能
        2.删除功能
        3.添加功能
        4.统计总分，求平均分
    -->
    <div id="app">
        科目：<input type="text" v-model.trim="subjects" placeholder="'请输入科目">
        分数：<input type="text" v-model.number="score" placeholder="请输入分数">
        <input @click="add()" type="submit" value="添加">

        <main style="margin-top: 20px;">
            <table border="1">
                <tr>
                    <th>编号</th>
                    <th>科目</th>
                    <th>成绩</th>
                    <th>操作</th>
                </tr>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>{{index+1}}</td>
                    <td>{{item.subjects}}</td>
                    <!-- 需求：不及格 就显示红的分数 -->
                    <td :class="{red:item.score<60}">{{item.score}}</td>
                    <td><a href="http://baidu.com" @click.prevent="del(item.id)">删除</a></td>
                </tr>
            </table>
            <p>总分:<span>{{total}}</span> 平均分：{{average}}</p>
        </main>
    </div>

    <script src="./script/vue.js"></script>
    <script>
        const app = new Vue({
            el: '#app',
            data: {
                subjects: '',
                score: '',
                list: [
                    { id: 1, subjects: '语文', score: 98 },
                    { id: 7, subjects: '数学', score: 20 },
                    { id: 12, subjects: '英语', score: 70 },
                ]
            },
            methods: {
                // 删除
                del(id) {
                    this.list = this.list.filter(item => item.id !== id);
                },
                //添加
                add() {
                    if (!this.subjects) {
                        alert('请输入科目');
                        return
                    }
                    if (typeof this.score !== 'number') {
                        alert('请输入正确的成绩')
                        return
                    }
                    this.list.unshift({
                        id: +new Date(),
                        subjects: this.subjects,
                        score: this.score
                    })
                    this.subjects = '',
                        this.score = ''
                }
            },
            computed: {
                total() {
                    return this.list.reduce((sum, item) => sum + item.score, 0)
                },
                average() {
                    // 正常写法
                    // let resule = this.list.reduce((sum, item) => sum + item.score, 0)
                    // return (resule / this.list.length).toFixed(2);

                    // 防止 NAN
                    if (this.list.length === 0) {
                        return 0;
                    }
                    // 简便写法  toFixed(2)保留两位小数
                    return (this.total / this.list.length).toFixed(2)
                }
            }
        })
    </script>
</body>

</html>